package com.jinsi.controller.yrf;

import com.jinsi.pojo.SyUser;
import com.jinsi.service.yrf.UserService;
import com.jinsi.service.yrf.AdministratorLoginLogService;
import com.jinsi.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * @author: 86185
 * @description: 登录业务
 * @date 2022/9/5
 */
@Controller
@Slf4j
public class LoginController {
    @Autowired
    @Qualifier(value = "redisTemplate")
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    /**
     * 添加登录日志
     **/
    @Autowired
    private AdministratorLoginLogService administratorLoginLogService;


//    /**
//     * 登录1
//     */
//    @ResponseBody
//    @RequestMapping("/login")
//    public R login(String captcha, String userName, String userPwd, HttpSession session){
//        R re = null;
//
//        String scaptcha =(String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
//
//        if(!captcha.equals(scaptcha)){
//            re = R.error("验证码错误");
//            return re;
//        }
//        SyUser user = userService.getUserByNameAndPwd(userName);
//        if(user != null){
//
//            String password1 = user.getUserPwd();
//            String password2 = DigestUtils.md5DigestAsHex((userName + userPwd).getBytes());
//            if (password1.equals(password2)){
//
//                session.setAttribute("loginuser",user);
//                /**
//                 * redisTemplate.opsForValue().set("loginuser",user,1, TimeUnit.MINUTES)
//                 * 过期时间1分钟
//                 */
//                try {
//                    administratorLoginLogService.insertadministratorLoginLog(user);
//                    System.out.println(user);
//                    re = R.custom(200,"登录成功",user);
//
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    re = R.error("异常");
//                }
//            }else {
//                re = R.error("密码错误");
//            }
//        }else{
//            re = R.error("用户名不存在");
//        }
//        return re;
//    }
//
    /**
     * 生成验证码
     * @return 状态码
     */
    @ResponseBody
    @RequestMapping("/yzm")
    public R getzym(){
        R re = null;
        Integer num = null;
                Long yzm = redisTemplate.opsForValue().getOperations().getExpire("yzm");
                if(yzm == -2){
                    num =  (int) ((Math.random() * 9 + 1) * 100000);
                    /**
                     * 过期时间1分钟
                     */
                    redisTemplate.opsForValue().set("yzm",num,1, TimeUnit.MINUTES);
                    re = R.ok("验证码发送成功");
                }else {
                    Long yzm5 = redisTemplate.opsForValue().getOperations().getExpire("yzm");
                    if(yzm5 > 0){
                        re = R.error("发送失败,验证码还有"+yzm5+"秒过期");
                    }
                }

        System.out.println(num);

       return re;
    }

    /**
     * 登录2
     */
    @ResponseBody
    @RequestMapping("/login2")
    public R login2(Integer yzm,String userPhone,HttpSession session){
        R re = null;
        /**
         * HttpSession  session = request.getSession()
         */
        Object yzms = redisTemplate.opsForValue().get("yzm");
        if(!yzm.equals(yzms)){
            re = R.error("验证码错误");
            return re;
        }else {
            SyUser user = userService.getUserByPhone(userPhone);
            if(user != null){

                if(yzms == null){
                    re = R.error("验证码不存在");
                }else {
                    session.setAttribute("loginuser",user);
                    /**
                     * redisTemplate.opsForValue().set("loginuser",user,1, TimeUnit.MINUTES)
                     * 过期时间1分钟
                     */
                    System.out.println(user);
                    re = R.custom(200,"登录成功!",user);
                }

            }else{
                re = R.error("请先创建用户");
            }
        }


        return re;
    }


    /**
     * 注册
     */
    @ResponseBody
    @RequestMapping("/redister")
    public R redister(@RequestBody SyUser syUser){
        R re = null;
        try {
            Integer userId = userService.addUser(syUser);
            Integer uId = userService.addUserRole(userId);
            re = R.ok("注册成功，请登录！");
            log.info("注册成功,账号为:"+userId);
            log.info("添加权限成功,账号为:"+uId);
        } catch (Exception e) {
            re = R.error("注册失败");
            log.error("注册失败error:",e);
        }

        return re;
    }

}
